<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Document</title>
    <style></style>
  </head>
  <body>
    <div>
      <h1>实现可迭代的接口</h1>
    </div>
    <script>
      //实现可迭代接口
      const obj = {
        store: ["foo", "bar", "baz"],

        [Symbol.iterator]: function () {
          let index = 0;
          const self = this;
          return {
            next: function () {
              const result = {
                value: self.store[index],
                done: index >= self.store.length,
              };
              index++;
              return result;
            },
          };
        },
      };

      for (const item of obj) {
        console.log(item); // foo  bar baz
      }
    </script>
  </body>
</html>
